package yawning.mew.character;

import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import yawning.mew.character.Glyph.Type;

public enum Clazz {
	WARRIOR("Warrior", 1),
	PALADIN("Paladin", 2),
	HUNTER("Hunter", 3),
	ROGUE("Rogue", 4),
	PRIEST("Priest", 5),
	DEATH_KNIGHT("Death Knight", 6),
	SHAMAN("Shaman", 7),
	MAGE("Mage", 8),
	WARLOCK("Warlock", 9),
	DRUID("Druid", 11);
	
	public static Clazz get(String name) {
		return valueOf(name.toUpperCase().replace(' ', '_'));
	}
	
	@Override
	public String toString() {
		return mName;
	}
	
	public final TalentTree[] getTalentTrees() {
		return mTrees.toArray(new TalentTree[0]);
	}
	
	public final Talent[] getTalents() {
		return mTalents.toArray(new Talent[0]);
	}
	
	public final Glyph[] getGlyphs() {
		return mGlyphs.toArray(new Glyph[0]);
	}
	
	private Clazz(String name, int num) {
		mName = name;
		mCid = num;
		loadData();
	}
	
	private String mName;
	private int mCid;
	private List<TalentTree> mTrees;
	private List<Talent> mTalents;
	private List<Glyph> mGlyphs;
	
	private void loadData() {
		mTrees = new ArrayList<TalentTree>();
		mTalents = new ArrayList<Talent>();
		mGlyphs = new ArrayList<Glyph>();
		
		try {
			URL url = new URL("http://www.wowarmory.com/talent-tree.xml?cid=" + mCid);
		    SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(url);
			Element page = doc.getRootElement();
			
			@SuppressWarnings("unchecked") List<Element> talentTrees = page.getChild("talentTrees").getChildren("tree");
			Element[] ordered = new Element[3];
			
			// Reorder the elements
			for (Element e : talentTrees) {
				ordered[Integer.parseInt(e.getAttributeValue("order"))] = e;
			}

			int talentId = 0;
			for (Element e : ordered) {
				TalentTree t = new TalentTree();
				t.mClass = this;
				t.mName = e.getAttributeValue("name");
				mTrees.add(t);

				@SuppressWarnings("unchecked") List<Element> tals = e.getChildren("talent");
				for (Element el : tals) {
					Talent tal = new Talent();
					
					tal.mClass = this;
					tal.mTree = t;;
					tal.mName = el.getAttributeValue("name");
					tal.mTalentNumber = talentId++;
					
					@SuppressWarnings("unchecked") List<Element> ranks = el.getChildren("rank");
					tal.mMaxRank = ranks.size();
					tal.mDescription = new String[tal.mMaxRank];
					for (Element rank : ranks) {
						int level = Integer.parseInt(rank.getAttributeValue("lvl"));
						tal.mDescription[level-1] = rank.getAttributeValue("description");
//						System.out.println(tal.mDescription[level-1]);
					}
					
					mTalents.add(tal);
					
					talentId++;
				}
			}
			
			url = new URL("http://www.wowarmory.com/search.xml?fl[source]=all&fl[type]=glyphs&fl[usbleBy]=" + mCid + "&fl[classType]=all&searchType=items");
		    URLConnection urlc = url.openConnection();
		    urlc.setRequestProperty("User-Agent", "Firefox/3.5");
			doc = builder.build(urlc.getInputStream());
			Element armorySearch = doc.getRootElement();
			@SuppressWarnings("unchecked") List<Element> items = armorySearch.getChild("searchResults").getChild("items").getChildren("item");
			for (Element e : items) {
				Glyph g = new Glyph();
				g.mName = e.getAttributeValue("name");
				String icon = e.getAttributeValue("icon");
				if (icon.contains("prime")) g.mType = Type.PRIME;
				else if (icon.contains("major")) g.mType = Type.MAJOR;
				else if (icon.contains("minor")) g.mType = Type.MINOR;
				mGlyphs.add(g);
				System.out.println(g.mName + g.mType);
			}
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
